import common.TreeNode;

import java.util.*;

/**
 * @author tmh
 * @date 2024/10/6 21:24
 * @description
 */
public class T199二叉树的右视图 {
    public List<Integer> rightSideView(TreeNode root) {
        if (root==null){
            return Collections.emptyList();
        }
        //继续使用层序遍历的做法来做
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.offer(root);
        List<Integer> list = new ArrayList<>();
        while (!deque.isEmpty()){
            int len = deque.size();
            while (len>0){
                TreeNode node = deque.poll();
                if (len==1){
                    list.add(node.val);
                }
                if (node.left!=null){
                    deque.offer(node.left);
                }
                if(node.right!=null){
                    deque.offer(node.right);
                }
                len--;
            }
        }
        return list;
    }
}
